package com.iplatform.auth.exception;

import com.alibaba.fastjson.JSON;
import com.iplatform.commons.utils.CommResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: Red
 * @CreateDate: 2020-04
 * @UpdateDate: 2020-04
 * @Description: 未登陆状态请求处理处理
 **/
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.setHeader("Content-Type", "application/json;charset=utf-8");
        httpServletResponse.setStatus(401);
        httpServletResponse.getWriter().print(JSON.toJSON(new CommResponse("请登录!", false)));
        httpServletResponse.getWriter().flush();
        httpServletResponse.getWriter().close();
    }
}
